﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CheckDB
{
    public partial class frmListMyProgram : Form
    {
        bool soft;
        string FileName = "";
        AcctionCheckBLL ActionBLL;
        AcctionCheckDAL ActionDAL;

        public frmListMyProgram(bool _soft)
        {
            InitializeComponent();
            soft = _soft;
            SearchFile(soft);
            AddToCheckListBox();
            if (checkedListBox1.Items.Count <= 0)
            {
                btnCapnhat.Enabled = false;
            }
            else
                btnCapnhat.Enabled = true;
        }

        List<string> files = new List<string>();
        public void SearchFile(bool soft)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo dv in allDrives)
            {
                DirectoryInfo dir = dv.RootDirectory;
                string Sys = Environment.GetFolderPath(Environment.SpecialFolder.System);
                if (soft)
                {
                    if (Sys.Contains(dir.FullName))
                    {
                        foreach (string name in Directory.GetFiles(dir.FullName, "VietKins.exe"))
                        {
                            files.Add(name);
                        }
                        foreach (string subDir in Directory.GetDirectories(dir.FullName))
                        {
                            if (!Sys.Contains(subDir))
                            {
                                try
                                {
                                    foreach (string name in Directory.GetFiles(subDir, "VietKins.exe", SearchOption.AllDirectories))
                                    {
                                        files.Add(name);
                                    }
                                }
                                catch //(Exception e)
                                {
                                    //MessageBox.Show("Lỗi SearchFile\n" + e.Message + "\n" + e.StackTrace + "\n" + e.Data);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (string name in Directory.GetFiles(dir.FullName, "VietKins.exe"))
                        {
                            files.Add(name);
                        }
                        foreach (string subDir in Directory.GetDirectories(dir.FullName))
                        {
                            try
                            {
                                foreach (string name in Directory.GetFiles(subDir, "VietKins.exe", SearchOption.AllDirectories))
                                {
                                    files.Add(name);
                                }
                            }
                            catch //(Exception e)
                            {
                                //MessageBox.Show("Lỗi SearchFile\n" + e.Message + "\n" + e.StackTrace + "\n" + e.Data);
                            }

                        }
                    }
                }
                else
                {
                    if (Sys.Contains(dir.FullName))
                    {
                        foreach (string name in Directory.GetFiles(dir.FullName, "VietEs.exe"))
                        {
                            files.Add(name);
                        }
                        foreach (string subDir in Directory.GetDirectories(dir.FullName))
                        {
                            if (!Sys.Contains(subDir))
                            {
                                try
                                {
                                    foreach (string name in Directory.GetFiles(subDir, "VietEs.exe", SearchOption.AllDirectories))
                                    {
                                        files.Add(name);
                                    }
                                }
                                catch //(Exception e)
                                {
                                    //MessageBox.Show("Lỗi SearchFile\n" + e.Message + "\n" + e.StackTrace + "\n" + e.Data);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (string name in Directory.GetFiles(dir.FullName, "VietEs.exe"))
                        {
                            files.Add(name);
                        }
                        foreach (string subDir in Directory.GetDirectories(dir.FullName))
                        {
                            try
                            {
                                foreach (string name in Directory.GetFiles(subDir, "VietEs.exe", SearchOption.AllDirectories))
                                {
                                    files.Add(name);
                                }
                            }
                            catch //(Exception e)
                            {
                                //MessageBox.Show("Lỗi SearchFile\n" + e.Message + "\n" + e.StackTrace + "\n" + e.Data);
                            }

                        }
                    }
                }
            }
        }

        public void AddToCheckListBox()
        {
            foreach (string str in files)
            {
                string path = str.Replace("VietKins.exe", "^").Replace("VietEs.exe", "^");
                path = path.Remove(path.Length - 1);
                if (File.Exists(path + "Manager.dll") && File.Exists(path + "Logininfo.txt"))
                {
                    checkedListBox1.Items.Add(str, false);
                }
            }
        }

        private void frmListMyProgram_Load(object sender, EventArgs e)
        {
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            foreach (object item in checkedListBox1.CheckedItems)
            {
                string path = item.ToString().Replace("VietKins.exe", "^").Replace("VietEs.exe", "^");
                path = path.Remove(path.Length - 1);
                ActionBLL = new AcctionCheckBLL(1, path);
                if (ActionBLL.CheckProcess(soft))
                {
                    return;
                }
                if (ActionBLL.SetPath(soft))
                    return;
                CheckData(sender, e);
                ActionBLL.GetConnectionString();
                Process(0, 150);
                if (ActionBLL.ExpandFolder(Path.GetDirectoryName(Application.ExecutablePath), FileName))
                    return;
                Process(150, 320);
                if (ActionBLL.ShowMessage("Bạn có muốn sao lưu lại dữ liệu trước khi cập nhật phiên bản mới nhất?", "Sao lưu") == DialogResult.Yes)
                {
                    if (ActionBLL.BackUp(soft))
                    {
                        if (ActionBLL.ShowMessage("Không sao lưu được dữ liệu.\n Nếu tiếp tục cập nhật có thể mất dữ liệu cũ.\n Có tiếp tục không?", "Cảnh báo") == DialogResult.No)
                            return;
                    }
                }
                Process(320, 580);
                ActionBLL.CopyTo(soft);
                Process(580, 620);
                ActionDAL.GetTablesTo();
                Process(620, 700);
                ActionDAL.CheckAndAddTable();
                Process(700, 810);
                ActionDAL.CheckAndAddColumn();
                Process(810, 980);
                ActionDAL.AddView();
                Process(980, 1000);
                ActionBLL.Complete();
            }
            Complete();
        }

        private void Process(int a, int b)
        {
            for (int i = a; i < b; i++)
            {
                //Làm cái gi` đó
                this.progressBar1.PerformStep();
            }
        }

        public void CheckData(object sender, EventArgs e)
        {
            if (soft)
            {
                ActionDAL = new AcctionCheckDAL();
                FileName = "Vietkins.zip";
            }
            else
            {
                ActionDAL = new AcctionCheckDAL(true);
                FileName = "VietEs.zip";
            }
            this.progressBar1.Visible = true;
            this.progressBar1.Maximum = 1000;
            this.progressBar1.Minimum = 0;
            this.progressBar1.Step = 1;
            this.btnCapnhat.Enabled = false;
        }

        public void Complete()
        {
            this.progressBar1.Value = 0;
            this.progressBar1.Visible = false;
            btnCapnhat.Enabled = true;
            ActionDAL.DisplayView();
            FileName = null;
            MessageBox.Show("Đã thao tác xong.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
    }
}
